<?php
/**
 * Dao of PromotionCondition
 *
 * @author fusq
 */
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class PromotionConditionDao extends BaseDao {

    protected $_name;

    protected $promotion;

    public function  _setup() {
        $this->_name = $this->prefix() . "promotion_conditions";
        $this->promotion = $this->prefix() . "promotions";
        parent::_setup();
    }

    public function updateFreeShippingPromotion($condition) {
        $where = $this->db_w->quoteInto("promotion_id in (select id from $this->promotion where promotion_type = 1)");
        $this->updateData($condition,$where);
    }
    public function getConditionBypromotionid($promotionid) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('promotion_id = ?', $promotionid);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    public function getConditionByType($promotionid,$type) {
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('promotion_id = ?', $promotionid);
        $select->where('promotion_condition_type = ?', $type);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    public function delCondition($promotionid) {
        $where = $this->db_w->quoteInto('promotion_id = ?', $promotionid);
        $this->delete($where);
    }
    public function deleteCondition($promotionid) {
        $data = array('is_delete' => 1);
        $where = $this->db_w->quoteInto('promotion_id = ? ', $promotionid);
        $this->updateData($data, $where);
    }
    
    public function getAllConditionByType($type,$promotionid){
    	$select = $this->db_r->select();
        $select->from($this->_name,"*");
        if($promotionid > 0){
            $select->where('promotion_id != ?', $promotionid);
        }
        $select->where('promotion_condition_type = ?', $type);
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
}

